package com.gmlive.common.ui.xui.alpha

import android.content.Context
import android.util.AttributeSet
import androidx.appcompat.widget.AppCompatButton
import com.gmlive.common.ui.xui.IAlphaViewHelper
import com.gmlive.common.ui.xui.helper.XUIAlphaViewHelper

/**
 * 在 pressed 和 disabled 时改变 View 的透明度
 *
 * @author xuexiang
 * @since 2018/11/30 下午1:12
 */
open class XUIAlphaButton : AppCompatButton {
    private var mAlphaViewHelper: IAlphaViewHelper? = null

    constructor(context: Context?) : super(context!!) {}
    constructor(context: Context?, attrs: AttributeSet?) : super(context!!, attrs) {}
    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(context!!, attrs, defStyleAttr) {}

    private val alphaViewHelper: IAlphaViewHelper
        private get() {
            if (mAlphaViewHelper == null) {
                mAlphaViewHelper = XUIAlphaViewHelper(this)
            }
            return mAlphaViewHelper!!
        }

    override fun setPressed(pressed: Boolean) {
        super.setPressed(pressed)
        alphaViewHelper.onPressedChanged(this, pressed)
    }

    override fun setEnabled(enabled: Boolean) {
        super.setEnabled(enabled)
        alphaViewHelper.onEnabledChanged(this, enabled)
    }

    /**
     * 设置是否要在 press 时改变透明度
     *
     * @param changeAlphaWhenPress 是否要在 press 时改变透明度
     */
    fun setChangeAlphaWhenPress(changeAlphaWhenPress: Boolean) {
        alphaViewHelper.setChangeAlphaWhenPress(changeAlphaWhenPress)
    }

    /**
     * 设置是否要在 disabled 时改变透明度
     *
     * @param changeAlphaWhenDisable 是否要在 disabled 时改变透明度
     */
    fun setChangeAlphaWhenDisable(changeAlphaWhenDisable: Boolean) {
        alphaViewHelper.setChangeAlphaWhenDisable(changeAlphaWhenDisable)
    }
}